package fr.p10.miage.rps.model;

/**
 * Created with IntelliJ IDEA.
 * User: naberrad
 * Date: 21/10/13
 * Time: 16:08
 * To change this template use File | Settings | File Templates.
 */
public class RockPaperScissors {

    public RockPaperScissors() {
    }

    public Result play(RPSEnum p1, RPSEnum p2) {
        if (p2.equals(p1)) {
            return Result.TIE;
        } else if (p1.equals(RPSEnum.PAPER) && p2.equals(RPSEnum.ROCK)
                || p1.equals(RPSEnum.ROCK) && p2.equals(RPSEnum.SCISSORS)
                || p1.equals(RPSEnum.SCISSORS) && p2.equals(RPSEnum.PAPER)) {
            return Result.WIN;
        } else {
            return Result.LOST;
        }
    }

    public Result play(Player p1, Player p2) {
        RPSEnum mov1, mov2;
        for (int i = 0; i < p1.getNbMove(); i++) {
            mov1 = p1.getNextMove();
            mov2 = p2.getNextMove();
            if (play(mov1, mov2).equals(Result.TIE)) {
                p2.setScore(p2.getScore() + 1);
                p1.setScore(p1.getScore() + 1);
            } else if (play(mov1, mov2).equals(Result.WIN)) {
                p1.setScore(p1.getScore() + 1);
            } else {
                p2.setScore(p2.getScore() + 1);
            }
        }

        if (p1.getScore() == p2.getScore())
            return Result.TIE;
        else if (p1.getScore() >= p2.getScore()) {
            return Result.WIN;
        } else {
            return Result.LOST;
        }
    }
}


